<?php

class BaseMapper {
	
	protected $keys = array();
	protected $moreMapping = array();
	protected $icon = false;
	protected $iconType = "cellphoneFlyer";
	
	function __construct() {
			        
    }   

	function attributesMapper($attributes){
		if (!$this->keys){
			$this->keys = $attributes->getKeys();
		}
		$return = array();
		foreach($this->keys as $key){
			if (is_array($key)){ 
				$auxVal =$attributes->$key["key"]; 								
				settype($auxVal , $key["type"]);					
				$return[$key["key"]] = $auxVal ;
			}
			else {
				$return[$key] = (string)$attributes->$key;
			}
		}
		return $return;
	}
	
	function objectMapper($object){		
		if ( is_array($object)){
			$return = array();
			foreach ($object as $obj){				
				$attributes = $this->attributesMapper($obj->attributes);
				if ($this->icon) { 
				 	$attributes["icon"] = $obj->getIcon($this->iconType);
				}
				$this->moreFunctions($obj, $attributes);
				if (isset($attributes["password"])){
					unset($attributes["password"]);
				}
				$return[] = $attributes;
			}
		}else{
			$return = $this->attributesMapper($object->attributes);
			$this->moreFunctions($object, $return);
		}
		
		return $return;
	}
	
	function moreFunctions($obj, &$attributes){
		if ($this->moreMapping){
			foreach ($this->moreMapping as $function){
				$this->$function($obj, $attributes);
			}
		}
	}
}

?>